﻿using System;
using System.Text;
using System.IO;
using PPI.General;

namespace PPI.Entities.Distribution
{
    public enum DistributionImageType
    {
        Pole,
        Meter,
        Transformer,
        Sectionalizer,
        Recloser,
        None
    }

    public class EDistributionPoleImage : EBasePosition
    {
        #region private members
        private Int32 m_Id;
        private string m_fileName;
        protected string m_TypeName;
        public string TempFileName { get; set; }
        public string PrefixFileName { get; set; }
        public int ParentId {get; set;}
        public string ParentPlaceMark {get;set;}
        public DistributionImageType ImageType {get;set;}
        public bool isProject {get;set;}
        public string ImageServerType {get; set; }

        #endregion

        #region Properties

        public Int32 Id
        {
            get
            {
                return m_Id;
            }
            set
            {
                m_Id = value;
            }
        }

        public string FileName
        {
            get { return m_fileName; }
            set { m_fileName = value; }
        }

        public string FullName {get;set;}

        #endregion

        #region Constructors

        public EDistributionPoleImage()
        {
            m_Id = Int32.MinValue;
        }

        public EDistributionPoleImage(Int32 id) : this()
        {
            m_Id = id;
        }

        #endregion

        #region Utilities
        public static byte[] ToBytes(string fileName)
        {
            FileStream _FileStream = null;
            BinaryReader _BinaryReader = null;
            try
            {
                _FileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                _BinaryReader = new BinaryReader(_FileStream);
                byte[] ImageBytes = _BinaryReader.ReadBytes((int)_FileStream.Length);
                return ImageBytes;

            }
            finally
            {
                _BinaryReader.Close();
                _FileStream.Close();
                _FileStream.Dispose();
            }
        }

        #region - Public methods -

        public void GenerateNewName(string prefix)
        {
            TempFileName = m_fileName;

            switch (ImageType)
            {
                case DistributionImageType.Pole:
                    m_fileName = "Photo_" + prefix + ".jpg";
                    break;
                case DistributionImageType.Meter:
                    m_fileName = "MeterPhoto_" + prefix + ".jpg";
                    break;

            }
        }

        #endregion

        #endregion

        #region Virtual Methods

        public override string GetPlacemarkId()
        {
            if (ImageType == DistributionImageType.Pole)
                return "DistributionPoleImage_" + Id;

            if (ImageType == DistributionImageType.Meter)
                return "DistributionMeterPoleImage_" + Id;

            return string.Empty;
        }

        protected override string GetPlaceMark()
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("<Placemark id=\"" + GetPlacemarkId() + "\">");
            s.AppendLine(" <Style>");
            s.AppendLine("     <BalloonStyle>");
            s.AppendLine("         <color>F9DDCD</color>");
            s.AppendLine("         <text>$[description]</text>");
            s.AppendLine("     </BalloonStyle>");
            s.AppendLine("</Style>");
            s.AppendLine(" <description>");
            s.AppendLine("     <![CDATA[" + GetHtmlPlacemark() + "]]>");
            s.AppendLine(" </description>");
            s.AppendLine("</Placemark>");
            return s.ToString();
        }

        public virtual string GenerateHTML()
        {
            throw new NotImplementedException();

        }

        public string GetImageUrl(Int32 parentId)
        {
            ConfigReader reader = ConfigReader.GetInstance();

            switch (ImageType)
            {
                case DistributionImageType.Pole:
                    m_TypeName = "DistributionInspection";
                    break;
                case DistributionImageType.Meter:
                    m_TypeName = "DistributionInspectionMeterImages";
                    break;
            }

            string url = "http://www.dev-poleposition.com/" + reader.ConectionMode + "/Images/" + m_TypeName + "/" + GetDirectoryName(parentId) + "/" + m_fileName;
            return url;
        }

        public string GetTemporaryFolder()
        {
            switch (ImageType)
            {
                case DistributionImageType.Pole:
                    m_TypeName = Constants.K_DISTRIBUTIONINSPECTIONFOLDERNAME;
                    break;
                case DistributionImageType.Meter:
                    m_TypeName = Constants.K_DISTRIBUTIONINSPECTIONMETERFOLDERNAME;
                    break;
            }

            return Constants.K_RepositoryFolder + "\\" + Constants.TempFolderName + "\\" + m_TypeName +"\\"+ FileName;
        }

        public string GetDirectoryName(Int32 parentId)
        {
            int Start = 0;
            int End = 99;
            string _Directory = "";
            bool _FoundPosition = false;
            while (!_FoundPosition)
            {
                if (parentId > Start && parentId > End)
                {
                    Start += 100;
                    End += 100;
                }
                else
                {
                    _FoundPosition = true;
                    _Directory = Start + "-" + End;
                }
            }
            return _Directory;
        }

        #endregion

        #region Override


        public override string GetHtmlPlacemark()
        {
            string htmlImage;
            
            if (isProject)
            {
                ConfigReader reader = ConfigReader.GetInstance();
                string mode = reader.ConectionMode;
                string directoryName = DirHelper.GetImageDirectory(ParentId, Id);
                string fileName = ParentId + "_" + Id;
                htmlImage =
                    "     <td><a href=\"http://www.dev-poleposition.com/Deploy/WebSite2.1/ImagePreview.aspx?Type=" + ImageServerType + "&mode=" +
                    mode + "&dn=" + directoryName + "&fn=" + fileName +
                    "&w=0&h=0\"><img src=\"http://www.dev-poleposition.com/Deploy/WebSite2.1/ImagePreview.aspx?Type=" + ImageServerType + "&mode=" +
                    mode + "&dn=" + directoryName + "&fn=" + fileName + "&w=0&h=0\"></a></td>";

            }
            else
            {
                htmlImage = "     <td><a href=\"#" + GetPlacemarkId() + ";balloon\"><img src=\"" + FullName + "\"></a></td>";
            }

            StringBuilder _Shtml = new StringBuilder();
            _Shtml.AppendLine("<table style='font-family:Verdana; font-size:10px;' border='0'>");
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine("     <td><a href=\"" + ParentPlaceMark + ";balloon\">Back</a></td>");
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine(" <tr>");
            _Shtml.AppendLine(htmlImage);
            _Shtml.AppendLine(" </tr>");
            _Shtml.AppendLine("</table>");
            return _Shtml.ToString();
        }

        public override bool Equals(object obj)
        {
            if (obj is EDistributionPoleImage)
            {
                EDistributionPoleImage i = obj as EDistributionPoleImage;
                // Return true if the fields match:
                return (m_Id == i.m_Id);
            }
            else
            {
                return false;
            }
        }

        public override string ToString()
        {
            return m_fileName;
        }
        
        #endregion
    }
}
